// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 *
 * @author: yanking
 * Create time: 2022-03-05 01:24
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.LeetCodeT.Greedy;

public class FindUnsortedSubarrayUpdateT {
    /**
     * 双指针一次遍历-> 根据数组元素局部有序的规律
     */
    public int findUnsortedSubarray(int[] nums) {
        int n = nums.length;
        int minn = Integer.MAX_VALUE, left = -1;
        int maxn = Integer.MIN_VALUE, right = -1;
        for (int i = 0; i < n; i++) {
            if (maxn > nums[i]) {
                right = i;
            } else {
                maxn = nums[i];
            }
            if (minn < nums[n - i - 1]) {
                left = n - i - 1;
            } else {
                minn = nums[n - i - 1];
            }

        }
        return right == -1 ? 0 : right - left + 1;
    }
}
